package gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;

/**
 * @author dotekien
 *         Date: Mar 28, 2006
 *         Time: 7:49:13 PM
 */
public class OpenFileAction extends AbstractAction {
    GUI gui;
    String command;

    public OpenFileAction(GUI gui, String command) {
        this.gui = gui;
        this.command = command;
    }

    public void actionPerformed(ActionEvent event) {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));

        chooser.setPreferredSize(new Dimension(600, 400));

        chooser.setMultiSelectionEnabled(true);
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        //chooser.setApproveButtonText("Dectect");
        chooser.setDialogTitle(command);

        JavaFileFilter filter = new JavaFileFilter("java");
        filter.setDescription("Java Source Files");
        chooser.addChoosableFileFilter(filter);

        //Equate set
        addEquatePanel(chooser);

        int option = chooser.showOpenDialog(gui);
        gui.repaint();
        if (option == JFileChooser.APPROVE_OPTION) {
            File[] sf = chooser.getSelectedFiles();
            gui.action(sf);
        }
    }

    private void addEquatePanel(JFileChooser chooser) {
        EquateSelection equatePanel = new EquateSelection(gui.getConfiguration());
        chooser.add(equatePanel, BorderLayout.LINE_END);
    }
}
